package com.jgp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jgp.common.CustomException;
import com.jgp.entity.Category;
import com.jgp.entity.Dish;
import com.jgp.entity.Setmeal;
import com.jgp.mapper.CateGoryMapper;
import com.jgp.service.CateGoryService;
import com.jgp.service.DishService;
import com.jgp.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class CateGoryServiceImpl extends ServiceImpl<CateGoryMapper, Category> implements CateGoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService sermealService;

    @Override
    public void remove(Long ids) {
//        查询当前分类是否关联了菜品和套餐，否则正常删除
        LambdaQueryWrapper <Dish> dishlambdaQueryWrapper = new LambdaQueryWrapper();
        dishlambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int count = dishService.count(dishlambdaQueryWrapper);
        if(count>0){
//            业务异常
            throw new CustomException("当前分类下关联了菜品不能删除");
        }

        LambdaQueryWrapper <Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<Setmeal> eq = setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int count1 = sermealService.count(eq);
        if(count1>0){
//            业务异常
            throw new CustomException("当前分类下关联了套餐不能删除");
        }
        super.removeById(ids);


    }

}
